using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MimeCloud.AssetManagement.Business;
using MimeCloud.AssetManagement.Business.Interface;
using MimeCloud.AssetManagement.Editor;

namespace MimeCloud.AssetManagement.AssetRendering.Metadata
{
    /// <summary>
    /// Interaction logic for MetadataGroupEdit.xaml
    /// </summary>
    public partial class MetadataGroupEdit : UserControl
    {
        public MetadataGroupEdit()
        {
            InitializeComponent();
        }

        public IList<IMetaData> ViewMetadata { get; set; }
        public IList<IMetaData> masterList { get; set; }
        public IMetaKey MetaKey { get; set; }

        private void btAddValueForLanguage_Click(object sender, RoutedEventArgs e)
        {
          
        }

        private void DisplayData()
        {
            MetadataValues.Children.Clear();
            foreach (var data in ViewMetadata)
            {
                MetadataItem itemView = new MetadataItem();
                itemView.SelectedData = data;
                MetadataValues.Children.Add(itemView);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (Languages.ItemsSource == null)
            {
                MetadataKey.Text = this.MetaKey.Name;
                Languages.Items.Clear();
                Languages.ItemsSource = AppCommon.Current.EnabledLanguages; DisplayData();
            }
           
        }

        private void Languages_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Languages.SelectedIndex >= 0 && e.AddedItems.Count > 0)
            {
                //create metadata
                var newMdValue = new MetaData();
                //set key
                newMdValue.MetaKey = MetaKey;

                //new value, set lang
                newMdValue.MetaKeyID = this.MetaKey.ID;
                newMdValue.MetaValue = new MetaValue {Value = String.Empty};
                newMdValue.MetaValue.Language = e.AddedItems[0] as ILanguage;

                //show editing dialog
                MetadataValueEditorDialog dlg = new MetadataValueEditorDialog();
                dlg.MetadataValue = newMdValue;
                if (dlg.ShowDialog() == true)
                {
                    //set new value
                    newMdValue.MetaValue.Value = dlg.AlteredValue;
                    masterList.Add(newMdValue);
                    ViewMetadata.Add(newMdValue);
                    
                    DisplayData();
                }
                Languages.SelectedIndex = -1;
            }
        }

        
    }
}
